package com.asm.DBloader;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ASM_fSalRepDS {
	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;
	
	
	public ASM_fSalRepDS(Context context) {
		this.context = context;
		dbHelper  = new ASM_DB(context);
		
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	public int Create(ASM_fSalRep det) 
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		
		
		ContentValues values = new ContentValues();
		
		values.put(ASM_DB.asmFsalrep_RepCode, det.getAsmFsalrep_RepCode());
		values.put(ASM_DB.asmFsalrep_RepName, det.getAsmFsalrep_RepName());
		values.put(ASM_DB.asmFsalrep_ASECode, det.getAsmFsalrep_ASECode());
	


		
		count = (int) dB.insert(ASM_DB.ASM_FSALREP, null, values);

		dB.close();
		return count;
		
	}
	
	public ArrayList<String> getsalrep() 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> repcode = new ArrayList<String>();
	
		String selectQuery = "select RepCode from fSalRep";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
	
			repcode.add(cursor.getString(0));

			
			
		}
		
		return repcode;
	}
	
	public ArrayList<String> getsalrepname(String Repcode) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> repcode = new ArrayList<String>();
	
		String selectQuery = "select RepName from fSalRep where Repcode = '"+Repcode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
	
			repcode.add(cursor.getString(0));
			
			
		}
		
		return repcode;
	}
	
	public ArrayList<String> getreplist(String ASMcode) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> repcode = new ArrayList<String>();
	
		String selectQuery = "select a.RepCode,sum(a.TotalAmt)as Totamt,b.RepName from FOrdHed as a inner join fSalRep as b on a.RepCode = b.RepCode inner join fASE as c on b.ASECode = c.ASECode where c.ASMCode = '"+ASMcode+"' group by b.RepName,a.RepCode";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
			BigDecimal decimal = new BigDecimal(cursor.getString(1));
			decimal = decimal.setScale(2, RoundingMode.HALF_UP);
	
			repcode.add(cursor.getString(2)+"-"+decimal.toString());
			
		}
		
		return repcode;
	}
	
	public ArrayList<String> getAllReps() 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> repcode = new ArrayList<String>();
	
		String selectQuery = "select RepCode from fSalRep";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
	
			repcode.add(cursor.getString(0));
			
			
		}
		
		return repcode;
	}
	
}
